﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WatiNCF.Automater
{
    public class AutomaterConfigHandler : ConfigurationSection
    {
        public AutomaterConfigHandler()
        {
        }

        [ConfigurationProperty("defaultController", IsRequired = true)]
        public string DefaultController
        {
            get { return (string)this["defaultController"]; }
        }

        [ConfigurationProperty("urls", IsRequired = true)]
        public UrlConfigElementCollection Urls
        {
            get { return this["urls"] as UrlConfigElementCollection; }
        }

        [ConfigurationProperty("controllers", IsRequired=true, IsDefaultCollection=true)]
        public ControllerConfigElementCollection Controllers
        {
            get { return this["controllers"] as ControllerConfigElementCollection; }
        }
    }

    public class ControllerConfigElementCollection : ConfigurationElementCollection
    {
        public ControllerConfigElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as ControllerConfigElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }

        }

        new public ControllerConfigElement this[string name]
        {
            get
            {
                return base.BaseGet(name) as ControllerConfigElement;
            }
            set
            {
                if (base.BaseGet(name) != null)
                {
                    base.BaseRemove(name);
                }
                this.BaseAdd(value);
            }

        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ControllerConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ControllerConfigElement)element).Name;
        }
    }

    public class ControllerConfigElement : ConfigurationElement
    {
        public ControllerConfigElement()
        {
        }

        public ControllerConfigElement(string name, string assembly, string type)
        {
            Name = name;
            Assembly = assembly;
            Type = type;
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
        
        [ConfigurationProperty("assembly", IsRequired = true)]
        public string Assembly
        {
            get { return (string)this["assembly"]; }
            set { this["assembly"] = value; }
        }

        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }
    }

    public class UrlConfigElementCollection : ConfigurationElementCollection
    {
        public UrlConfigElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as UrlConfigElement;
            }
        }

        new public UrlConfigElement this[string path]
        {
            get
            {
                return base.BaseGet(path) as UrlConfigElement;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlConfigElement)element).Path;
        }
    }

    public class UrlConfigElement : ConfigurationElement
    {
        public UrlConfigElement()
        {
        }

        public UrlConfigElement(string path)
        {
            Path = path;
        }

        [ConfigurationProperty("path", IsRequired = true)]
        public string Path
        {
            get { return (string)this["path"]; }
            set { this["path"] = value; }
        }
    }
}
